package com.zdp.applet.rabbit;

import com.zdp.applet.config.AppProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * @author sesshomaru
 * @date 2021/7/10 0:19
 * rabbit mq 初始化配置，用于构建交换机和队列
 */
@Slf4j
@Configuration
public class RabbitMQInitializationConfig {

    @Autowired
    private AppProperties appProperties;


    @Bean
    public DirectExchange testExchange() {
        return ExchangeBuilder.directExchange("test.ex").build();
    }

    @Bean
    public Queue testPopupQueue() {
        return QueueBuilder.durable("test.queue").build();
    }

    @Bean
    public Binding testPopupBinding() {
        return BindingBuilder.bind(testPopupQueue()).to(testExchange()).with("test.queue");
    }


    @Bean
    public ConnectionFactory connectionFactory() {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
        connectionFactory.setHost(appProperties.getRabbit().getHost());
        connectionFactory.setPort(appProperties.getRabbit().getPort());
        connectionFactory.setUsername(appProperties.getRabbit().getUsername());
        connectionFactory.setPassword(appProperties.getRabbit().getPassword());
        connectionFactory.setVirtualHost("/");
        // 开启发布确认模式
        connectionFactory.setPublisherConfirmType(CachingConnectionFactory.ConfirmType.SIMPLE);
        connectionFactory.setPublisherReturns(true);
        connectionFactory.createConnection();
        return connectionFactory;
    }

    @Bean
    public RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory) {
        RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);
        rabbitAdmin.setAutoStartup(true);
        return rabbitAdmin;
    }

    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        // 消息返回确认一定要开启的
        rabbitTemplate.setMandatory(true);

        // 返回消息
        rabbitTemplate.setReturnCallback((message, replyCode, replyText, exchange, routingKey) -> {
            log.info("returnCallback:{}, replyCode:{}, replyText:{}, exchange:{}, routingKey:{}",
                    message, replyCode, replyText, exchange, routingKey);
        });

        // 确认消息
        rabbitTemplate.setConfirmCallback((correlationData, ack, cause) ->
                log.info("correlationData:{}, ack:{}, cause:{}",
                        correlationData, ack, cause));
        return rabbitTemplate;
    }

    // 不配置这个默认就是自动签收
    @Bean
    public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(ConnectionFactory connectionFactory) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setAcknowledgeMode(AcknowledgeMode.MANUAL);
        factory.setMessageConverter(new Jackson2JsonMessageConverter());
        return factory;
    }
}
